package com.jintoufs.web.action.hotel;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.dao.hotel.HotelOrderDetailMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.hotel.Hotel;
import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.hotel.HotelOrderDetail;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.scene.SceneOrderDetail;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.divided.DividedService;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.web.action.refund.RefundAction;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.apache.commons.lang.StringUtils;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("hotel/order")
public class HotelOrderAction extends SupportAction{
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private RefundAction refundAction;
    @Resource
    private HotelOrderDetailMapper hotelOrderDetailMapper;
    @Resource
    private DividedService dividedService;
    @Resource
    private CustomerService customerService;
    @Resource
    private ShopService shopService;
    @Resource
    private AccountDetailService accountDetailService;
    @RequestMapping("list")
    public String list(Model model,String code ,String name,String startDate,String endDate) {
        model.addAttribute("code",code);
        model.addAttribute("name",name);
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        return "hotel/order/list";
    }

    @RequestMapping("query")
    @ResponseBody
    @SystemControllerLog(description = "获取酒店订单列表")
    public JqgridPage query(int page,
                        int rows, String sidx,
                        String sord,
                        String filters,String code ,String name,String startDate,String endDate,
                        @CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        if(StringUtils.isNotEmpty(code)){
            QueryRule role = new QueryRule();
            role.setField("ho.id");
            role.setOp("cn");
            role.setData(code);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(name)){
            QueryRule role = new QueryRule();
            role.setField("cust.mobile_phone");
            role.setOp("cn");
            role.setData(name);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(startDate)){
            QueryRule role = new QueryRule();
            role.setField("ho.in_time");
            role.setOp("ge");
            role.setData(startDate);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(endDate)){
            QueryRule role = new QueryRule();
            role.setField("ho.in_time");
            role.setOp("le");
            role.setData(endDate);
            filters_.getRules().add(role);
        }

        filters_.setSidx(sidx);
        filters_.setSord(sord);
        QueryRule role = new QueryRule();
        role.setField("sm.customer_id");
        role.setOp("eq");
        role.setData(customer.getId());
        filters_.getRules().add(role);

        List<Map<String,Object>> hotels = hotelOrderService.queryByFilters(filters_);
        PageInfo pageInfo = new PageInfo(hotels);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("confirm")
    @ResponseBody
    @Transactional
    public AjaxMsg confirm(String id,@CurrentUser Customer customer){
        AjaxMsg msg = new AjaxMsg();
        try {
            hotelOrderService.payConfirm(id);


            msg.setCode("200");
            msg.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(AjaxMsg.FAILURE);
        }
        return msg;
    }

    @RequestMapping(value="/detail")
    public String detail(String id, Model model) {
        Map<String,Object> param = new HashMap<>();
        HotelOrderDetail hotelOrderDetail = hotelOrderDetailMapper.selectByHotelOrderId(id).get(0);
        HotelOrder hotelOrder = hotelOrderService.getById(id);
        model.addAttribute("orderDetail", hotelOrderDetail);
        model.addAttribute("hotelOrder", hotelOrder);
        return "hotel/order/detail";
    }
//    @RequestMapping("refund")
//    @ResponseBody
//    public AjaxMsg refund(String id){
//        AjaxMsg msg = new AjaxMsg();
//        msg.setCode(AjaxMsg.SUCCESS);
//        msg.setMessage("成功");
//        try {
//            hotelOrderService.refund(id);
//        } catch (Exception e) {
//            msg.setCode(AjaxMsg.FAILURE);
//            msg.setMessage(e.getMessage());
//        }
//        return msg;
//    }

    @RequestMapping("verify")
    @ResponseBody
    public AjaxMsg verify(String id){
        AjaxMsg ajaxMsg = new AjaxMsg();
        HotelOrder hotelOrder = hotelOrderService.getById(id);
        if (DateUtils.intervalDays(hotelOrder.getModifyTime(),new Date())>1&&hotelOrder.getStatus().equals(Constants.ORDER_STATUS_CONFIRM)) {
            dividedService.doDivided(hotelOrder,getUUID(),"hotel");
        }else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("当前订单状态不可核销，请检查订单已确认或订单处于确认状态已经一天！");
        }

        return ajaxMsg;
    }
}
